package com.wjh.graduation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjh.graduation.domain.Bill;
import com.wjh.graduation.domain.Column;
import com.wjh.graduation.domain.R;
import com.wjh.graduation.service.BillService;
import com.wjh.graduation.util.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.validation.constraints.NotNull;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created on 2022/2/10.
 *
 * @author Wu Jiahong
 */
@Api(tags = "账单的柱状图API")
@RestController
@RequestMapping("/billChart")
public class BillChartController {

	@Autowired
	BillService billService;

	@Autowired
	Util util;

	@GetMapping("/getColumn")
	@ApiOperation("获得时间近三天消费情况")
	public R<List<Column>> getColumn(@NotNull @RequestParam @ApiParam("账单时间，格式2022-1-2") @DateTimeFormat(pattern = "yyyy-MM-dd") Date time, @NotNull @RequestParam @ApiParam("用户ID") Integer userId) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		//获得昨天时间,注意转为字符串
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		String yesterday = util.dateToString(calendar.getTime());
		//获得前天时间,注意转为字符串
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		String bYesterday = util.dateToString(calendar.getTime());
		//获得今天时间,注意转为字符串
		String today = util.dateToString(time);
		//创建两个存放目标值的List和存放Column的list(返回结果)
		List<Column> list = new ArrayList<>();
		List<Double> inList = new ArrayList<>();
		List<Double> outList = new ArrayList<>();
		//创建返回列表对象
		Column column1 = new Column();
		Column column2 = new Column();
		column1.setName("支出");
		column2.setName("收入");
		//查询今天
		QueryWrapper<Bill> queryWrapperIn = new QueryWrapper<>();
		queryWrapperIn.eq("user_id", userId).eq("time", today).eq("type", 1);
		QueryWrapper<Bill> queryWrapperOut = new QueryWrapper<>();
		queryWrapperOut.eq("user_id", userId).eq("time", today).eq("type", 0);
		List<Bill> list11 = billService.list(queryWrapperIn);
		List<Bill> list10 = billService.list(queryWrapperOut);
		//今天收入/支出总和插入
		inList.add(list11.stream().mapToDouble(Bill::getMoney).sum());
		outList.add(list10.stream().mapToDouble(Bill::getMoney).sum());
		//查询昨天
		QueryWrapper<Bill> queryWrapperInY = new QueryWrapper<>();
		queryWrapperInY.eq("user_id", userId).eq("time", yesterday).eq("type", 1);
		QueryWrapper<Bill> queryWrapperOutY = new QueryWrapper<>();
		queryWrapperOutY.eq("user_id", userId).eq("time", yesterday).eq("type", 0);
		List<Bill> list21 = billService.list(queryWrapperInY);
		List<Bill> list20 = billService.list(queryWrapperOutY);
		//昨天收入/支出总和插入
		inList.add(list21.stream().mapToDouble(Bill::getMoney).sum());
		outList.add(list20.stream().mapToDouble(Bill::getMoney).sum());

		//查询前天
		QueryWrapper<Bill> queryWrapperInB = new QueryWrapper<>();
		queryWrapperInB.eq("user_id", userId).eq("time", bYesterday).eq("type", 1);
		QueryWrapper<Bill> queryWrapperOutB = new QueryWrapper<>();
		queryWrapperOutB.eq("user_id", userId).eq("time", bYesterday).eq("type", 0);
		List<Bill> list31 = billService.list(queryWrapperInB);
		List<Bill> list30 = billService.list(queryWrapperOutB);
		//今天收入/支出总和插入
		inList.add(list31.stream().mapToDouble(Bill::getMoney).sum());
		outList.add(list30.stream().mapToDouble(Bill::getMoney).sum());
		//设置好返回对象数组
		column1.setData(inList);
		column2.setData(outList);
		list.add(column1);
		list.add(column2);
		//加到
		return new R(200, list, "获取成功");
	}


	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		//转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		// CustomDateEditor为自定义日期编辑器
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
}
